<?php

namespace App\Models;

/**
 * App\Models\Article
 *
 * @property int $id
 * @property string $thumb 封面图
 * @property string $title 标题
 * @property int $cate_id 分类ID
 * @property string $author 编辑人员
 * @property string $source 来源
 * @property string $source_link 来源链接
 * @property string $describe 摘要
 * @property int $status 状态1正常2锁定
 * @property string|null $content 内容
 * @property int $good 推荐：1否2是
 * @property int $view 浏览量
 * @property \Illuminate\Support\Carbon|null $created_at
 * @property \Illuminate\Support\Carbon|null $updated_at
 * @property-read \App\Models\ArticleCategory $category
 * @method static \Illuminate\Database\Eloquent\Builder|Article newModelQuery()
 * @method static \Illuminate\Database\Eloquent\Builder|Article newQuery()
 * @method static \Illuminate\Database\Eloquent\Builder|Article query()
 * @method static \Illuminate\Database\Eloquent\Builder|Article whereAuthor($value)
 * @method static \Illuminate\Database\Eloquent\Builder|Article whereCateId($value)
 * @method static \Illuminate\Database\Eloquent\Builder|Article whereContent($value)
 * @method static \Illuminate\Database\Eloquent\Builder|Article whereCreatedAt($value)
 * @method static \Illuminate\Database\Eloquent\Builder|Article whereDescribe($value)
 * @method static \Illuminate\Database\Eloquent\Builder|Article whereGood($value)
 * @method static \Illuminate\Database\Eloquent\Builder|Article whereId($value)
 * @method static \Illuminate\Database\Eloquent\Builder|Article whereSource($value)
 * @method static \Illuminate\Database\Eloquent\Builder|Article whereSourceLink($value)
 * @method static \Illuminate\Database\Eloquent\Builder|Article whereStatus($value)
 * @method static \Illuminate\Database\Eloquent\Builder|Article whereThumb($value)
 * @method static \Illuminate\Database\Eloquent\Builder|Article whereTitle($value)
 * @method static \Illuminate\Database\Eloquent\Builder|Article whereUpdatedAt($value)
 * @method static \Illuminate\Database\Eloquent\Builder|Article whereView($value)
 * @mixin \Eloquent
 */
class Article extends Base
{
    protected $table = 'articles';

    const STATUS_1 = 1;
    const STATUS_2 = 2;

    const STATUS_TEXT = [
        self::STATUS_1 => '<span class="el-tag">正常</span>',
        self::STATUS_2 => '<span class="el-tag el-tag--danger">锁定</span>',
    ];

    const GOOD_1 = 1;
    const GOOD_2 = 2;

    const GOOD_TEXT = [
        self::GOOD_1 => '<span class="el-tag">否</span>',
        self::GOOD_2 => '<span class="el-tag  el-tag--danger">是</span>',
    ];

    protected $fillable = [
        'id','title','content','thumb','cate_id','describe','status',
        'good','view','created_at','updated_at','author','source','source_link'
    ];


    public function category(){
        return $this->belongsTo('App\Models\ArticleCategory', 'cate_id', 'id');
    }

    /**
     * 获取列表
     * @param array $params 条件参数
     * @param array $fields 查询的字段
     * @return mixed
     */
    protected function getList($params = [], $fields = ['*']){
        $lists = $this->select($fields)
            ->where(function ($query) use ($params) {
                if(isset($params['kwd']) && $params['kwd']) {
                    $query ->where('title','like','%' . $params['kwd'] . '%');
                }
                if(isset($params['status']) && $params['status']) {
                    $query ->where('status',$params['status']);
                }
                if(isset($params['good']) && $params['good']) {
                    $query ->where('good',$params['good']);
                }
                if(isset($params['cid']) && $params['cid']) {
                    $query ->where('cate_id',$params['cid']);
                }
            })->with(['category' => function ($query) {
                $query->select(['id','name']);
            }])->orderBy('created_at', 'desc')
            ->paginate(config('app.limit'));

        $this->stateToText($lists,[
            'status'=> self::STATUS_TEXT,
            'good'  => self::GOOD_TEXT,
        ]);
        return $lists;
    }

}
